package com16listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import tool.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class servletContextListener implements javax.servlet.ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Map map=new HashMap();
        for (int i=0;i<20;i++)
        {
            Connection connection=null;
            try {
                connection=jdbctool.getconection();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            map.put(connection,true);
        }
        ServletContext servletContext=sce.getServletContext();
        servletContext.setAttribute("key1",map);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext servletContext=sce.getServletContext();
        Map<Connection, Boolean> map= (Map) servletContext.getAttribute("key1");
        for (Connection c: map.keySet()
             ) {
            if (c!=null)
            jdbctool.close(c,null,null);
        }
    }
}
